package net.ibizsys.eclipse.studio.dialog;

import net.ibizsys.eclipse.studio.Activator;
import net.ibizsys.eclipse.studio.api.APIManager;
import net.ibizsys.eclipse.studio.api.ActionResult;
import net.ibizsys.eclipse.studio.data.IBizUser;
import net.ibizsys.eclipse.studio.data.LocalDataManager;
import net.ibizsys.eclipse.studio.i18n.I18n;
import net.ibizsys.eclipse.studio.util.StringKit;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * 登录界面窗口
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public class LoginViewDialog extends BaseViewDialog
{
	private Text txtHost;
	private Text txtAccount;
	private Text txtPassword;

	public LoginViewDialog(Shell parentShell)
	{
		super(parentShell);
	}

	@Override
	protected Control createDialogArea(Composite parent)
	{
		getShell().setText(I18n.LoginTitle);

		Composite container = (Composite) super.createDialogArea(parent);

		GridData griddata = new GridData(GridData.FILL_BOTH);
		griddata.widthHint = defWidth();
		container.setLayoutData(griddata);

		createLoginArea(container);

		return container;
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent)
	{
		createButton(parent, IDialogConstants.OK_ID, I18n.Close, false);
	}

	/**
	 * 创建登录区
	 * 
	 * @param compsoite
	 */
	private void createLoginArea(Composite compsoite)
	{
		IBizUser user = Activator.getDefault().curUser();

		Group group = new Group(compsoite, SWT.NONE);
		group.setText(I18n.Login);

		GridData griddata = new GridData(GridData.FILL_HORIZONTAL);
		griddata.heightHint = defHeight();
		group.setLayoutData(griddata);

		group.setLayout(new GridLayout(4, false));

		Label serverLabel = new Label(group, SWT.NONE);
		serverLabel.setText(I18n.ServerPath);
		txtHost = new Text(group, SWT.NONE);
		txtHost.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
		txtHost.setText(user.getRemoteUrl());

		Label accountLabel = new Label(group, SWT.NONE);
		accountLabel.setText(I18n.Account);
		txtAccount = new Text(group, SWT.NONE);
		txtAccount.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
		txtAccount.setText(user.getAccount());

		Label pwdLabel = new Label(group, SWT.NONE);
		pwdLabel.setText(I18n.Password);
		txtPassword = new Text(group, SWT.NONE | SWT.PASSWORD);
		txtPassword.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
		txtPassword.setEchoChar('*');
		txtPassword.setText(user.getPassword());

		Composite composite = new Composite(group, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
		composite.setLayout(new GridLayout(2, false));

		infoLabel = new Label(composite, SWT.NONE);
		infoLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 2, 1));
		infoLabel.setForeground(compsoite.getDisplay().getSystemColor(SWT.COLOR_RED));

		Button loginButton = new Button(group, SWT.PUSH);
		loginButton.setText(I18n.Login);
		loginButton.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseUp(MouseEvent e)
			{
				onLoginClicked();
			}
		});

		Button resetButton = new Button(group, SWT.PUSH);
		resetButton.setText(I18n.Reset);
		resetButton.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseUp(MouseEvent e)
			{
				onResetClicked();
			}
		});
	}

	private void onLoginClicked()
	{
		if (isBusy())
		{
			return;
		}

		final String strHost = txtHost.getText();
		final String strAccount = txtAccount.getText();
		final String strPWD = txtPassword.getText();

		if (StringKit.IsBlank(strHost) || StringKit.IsBlank(strAccount) || StringKit.IsBlank(strPWD))
		{
			alert(I18n.Error, I18n.EmptyError);
			return;
		}
		showHUD(I18n.Logining);

		new Thread()
		{
			@Override
			public void run()
			{
				try
				{
					ActionResult<IBizUser> callResult = APIManager.login(IBizUser.class, strHost, strAccount, strPWD);

					hideHUD();

					if (!callResult.isOK())
					{
						alert(I18n.Error, callResult.getErrorInfo());
					} else
					{
						IBizUser user = callResult.getData();
						user.setPassword(strPWD);
						user.setRemoteUrl(strHost);
						user.setAccount(strAccount);

						LocalDataManager.manager().saveUser(user);
						Activator.getDefault().login(user);

						LoginViewDialog.this.closeAndOpenMainView();
					}
				} catch (Exception ex)
				{
					alert(I18n.Error, I18n.SaveLocalError);
				}
			}
		}.start();
	}

	/**
	 * 点击重置按钮
	 */
	private void onResetClicked()
	{
		txtHost.setText("");
		txtAccount.setText("");
		txtPassword.setText("");
		infoLabel.setText("");
	}

	/**
	 * 关闭当前并打开主界面
	 */
	private void closeAndOpenMainView()
	{
		runOnUIThread(new Runnable()
		{
			@Override
			public void run()
			{
				close();
				new MainViewDialog(getShell()).open();
			}
		});
	}

	/**
	 * 默认窗口宽度
	 * 
	 * @return
	 */
	private int defWidth()
	{
		return (int) (Activator.WindowWidth * Activator.screenRate(getShell()));
	}

	/**
	 * 默认窗口高度
	 * 
	 * @return
	 */
	private int defHeight()
	{
		return (int) (Activator.WindowHeight * Activator.screenRate(getShell()));
	}
}
